package com.yungu.swift.api.route.v2.passenger;

import com.alibaba.dubbo.config.annotation.Reference;
import com.yungu.swift.api.base.BaseApi;
import com.yungu.swift.api.base.gateway.CurrentUser;
import com.yungu.swift.assets.passenger.model.dto.PassengerEmergencyDto;
import com.yungu.swift.assets.passenger.model.dto.PassengerEmergencyMemberDto;
import com.yungu.swift.assets.passenger.model.vo.PassengerEmergencyVo;
import com.yungu.swift.assets.passenger.service.PassengerEmergencyMemberService;
import com.yungu.swift.assets.passenger.service.PassengerEmergencyService;
import com.yungu.swift.base.ResponseData;
import com.yungu.swift.base.model.api.ApiParam;
import com.yungu.swift.utils.DateUtils;
import com.yungu.swift.utils.MapUtils;
import com.yungu.swift.utils.ParamUtil;
import com.yungu.swift.utils.ValidateUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;
import java.util.Map;

import static com.yungu.swift.api.base.ApiErrorEnum.PARAM_ERROR;

/**
 * 行程安全-紧急联系人相关api
 *
 * @author Zerrion
 * @version 1.0.0-RELEASE
 * @since 2018-10-16 11:27
 */
@RestController
@RequestMapping(value = "/api/v2/passenger/token/emergency")
@Api(value = "【乘客】【紧急联系人】紧急联系人接口V2", tags = "V2-紧急联系人接口")
public class PasEmergencyApi extends BaseApi {

    @Reference
    private PassengerEmergencyService passengerEmergencyService;
    @Reference
    private PassengerEmergencyMemberService passengerEmergencyMemberService;


    @ApiOperation(value = "新增紧急联系成员接口")
    @PostMapping(value = {"/addMember"})
    public ResponseData<String> addMember(@RequestBody Map<String, String> paramMap, @CurrentUser ApiParam apiParam) {
        // 紧急联系成员名称
        String memberName = paramMap.get("memberName");
        // 紧急联系成员手机号
        String memberMobile = paramMap.get("memberMobile");
        if (ParamUtil.isIllegal(memberName, memberMobile)) {
            return ResponseData.buildErrorResponse(PARAM_ERROR.getCode(), PARAM_ERROR.getMsg());
        }
        if (!ValidateUtils.validateMobile(memberMobile)) {
            return ResponseData.buildErrorResponse(ResponseData.ERROR_CODE, "手机号码格式错误");
        }
        return passengerEmergencyService.addMember(apiParam.getUserUuid(), memberName, memberMobile);
    }

    @ApiOperation(value = "删除紧急联系成员接口")
    @PostMapping(value = {"/delMember"})
    public ResponseData<String> delMember(@RequestBody Map<String, String> paramMap) {
        // 紧急联系人uuid
        String emergencyUuid = paramMap.get("emergencyUuid");
        // 紧急联系成员 uuid
        String memberUuid = paramMap.get("memberUuid");
        return passengerEmergencyService.delMember(emergencyUuid, memberUuid);
    }

    @ApiOperation(value = "更新紧急联系成员信息接口")
    @PostMapping(value = {"/updateMember"})
    public ResponseData<String> updateMember(@RequestBody Map<String, String> paramMap) {
        // 紧急联系人uuid
        String emergencyUuid = paramMap.get("emergencyUuid");
        // 紧急联系成员 uuid
        String memberUuid = paramMap.get("memberUuid");
        // 紧急联系成员姓名
        String memberName = paramMap.get("memberName");
        // 紧急联系成员手机号
        String memberMobile = paramMap.get("memberMobile");
        if (ParamUtil.isIllegal(emergencyUuid, memberUuid, memberName, memberMobile)) {
            return ResponseData.buildErrorResponse(PARAM_ERROR.getCode(), PARAM_ERROR.getMsg());
        }
        if (!ValidateUtils.validateMobile(memberMobile)) {
            return ResponseData.buildErrorResponse(ResponseData.ERROR_CODE, "手机号码格式错误");
        }
        return passengerEmergencyService.updateMember(emergencyUuid, memberUuid, memberName, memberMobile);
    }


    @ApiOperation(value = "切换紧急自动分享行程信息接口")
    @PostMapping(value = {"/updateStatus"})
    public ResponseData<Integer> updateStatus(@RequestBody Map<String, Object> paramMap) {
        // 紧急联系人uuid
        Object emergencyUuid = paramMap.get("emergencyUuid");
        // 成员状态
        Object memberStatus = paramMap.get("memberStatus");
        // 生效时间段开始
        Object emergencyTimeStart = paramMap.get("emergencyTimeStart");
        // 生效时间段结束
        Object emergencyTimeEnd = paramMap.get("emergencyTimeEnd");
        if (ParamUtil.isIllegal(emergencyUuid, memberStatus, emergencyTimeStart, emergencyTimeEnd)) {
            return ResponseData.buildErrorResponse(PARAM_ERROR.getCode(), PARAM_ERROR.getMsg());
        }
        Date start = null;
        Date end = null;
        try {
            start = DateUtils.parse(emergencyTimeStart.toString(), "HH:mm:ss");
            end = DateUtils.parse(emergencyTimeEnd.toString(), "HH:mm:ss");
        } catch (Exception e) {
            return ResponseData.buildErrorResponse(ResponseData.ERROR_CODE, "时间格式错误");
        }
        return passengerEmergencyService.updateStatus(emergencyUuid.toString(), Integer.valueOf(memberStatus.toString())
                , start, end);
    }


    @ApiOperation(value = "切换紧急成员自动分享行程信息接口")
    @PostMapping(value = {"/updateMemberStatus"})
    public ResponseData<Integer> updateMemberStatus(@RequestBody Map<String, Object> paramMap) {
        // 紧急联系人uuid
        Object emergencyUuid = paramMap.get("emergencyUuid");
        // 成员 uuid
        Object memberUuid = paramMap.get("memberUuid");
        // 成员状态
        Object memberStatus = paramMap.get("memberStatus");
        if (ParamUtil.isIllegal(emergencyUuid, memberUuid, memberStatus)) {
            return ResponseData.buildErrorResponse(PARAM_ERROR.getCode(), PARAM_ERROR.getMsg());
        }
        return passengerEmergencyService.updateMemberStatus(emergencyUuid.toString(), memberUuid.toString(), Integer.valueOf(memberStatus.toString()));
    }


    @ApiOperation(value = "紧急联系人是否设置接口")
    @PostMapping(value = {"/settingInfo"})
    public ResponseData<Map<String, Object>> settingInfo(@CurrentUser ApiParam apiParam) {
        PassengerEmergencyDto emergencyDto = passengerEmergencyService.get(MapUtils.build("passengerUuid", apiParam.getUserUuid())).getData();
        List<PassengerEmergencyMemberDto> listMember = passengerEmergencyMemberService.list(MapUtils.build("passengerUuid", apiParam.getUserUuid())).getData();
        Map<String, Object> resultMap = MapUtils.build(2);
        resultMap.put("emergencyStatus", true);
        resultMap.put("isSet", true);
        if (null == emergencyDto || PassengerEmergencyDto.EMERGENCY_STATUS_INVALID == emergencyDto.getEmergencyStatus()) {
            resultMap.put("emergencyStatus", false);
        }
        if (CollectionUtils.isEmpty(listMember)) {
            resultMap.put("isSet", false);
        }
        return ResponseData.buildSuccessResponse("紧急联系人是否设置获取成功", resultMap);
    }


    @ApiOperation(value = "紧急联系人详情信息接口")
    @PostMapping(value = {"/emergencyInfo"})
    public ResponseData<PassengerEmergencyVo> emergencyInfo(@CurrentUser ApiParam apiParam) {
        return passengerEmergencyService.emergencyInfo(apiParam.getUserUuid());
    }
}
